package com.itheima.service;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.pageBean;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;


@Component("brandService")
public class BrandServiceImpl implements BrandService {

    private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();






    @Override
    public List<Brand> selectAll() {

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectAll();

        sqlSession.close();

        return brands;
    }

    @Override
    public void Add(Brand brand) {

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.Add(brand);
        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public void deleteByIds(int[] ids) {

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteByIds(ids);

        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public pageBean<Brand> selectByPage(int page, int rows) {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int begin = (page-1)*rows;

        List<Brand> brands = mapper.selectByPage(begin, rows);


        int count = mapper.selectTotalCount();

        pageBean<Brand> pageBean = new pageBean<>();

        pageBean.setPage(brands);

        pageBean.setTotal(count);
        System.out.println("pagebean:   " +pageBean);

        sqlSession.close();

        return pageBean;
    }

    @Override
    public pageBean<Brand> selectByPageAndCondition(int page, int rows, Brand brand) {

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int begin = (page-1)*rows;

        String brandName = brand.getBrandName();

        if(brandName!=null && brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
            System.out.println(brand.getBrandName());
        }

        String CompanyName = brand.getCompanyName();

        if(CompanyName!=null && CompanyName.length()>0){
            brand.setCompanyName("%"+CompanyName+"%");
            System.out.println(brand.getCompanyName());
        }

        List<Brand> brands = mapper.selectByPageAndCondition(begin, rows,brand);

        int i = mapper.selectTotalCountByBrand(brand);

        pageBean<Brand> pageBean = new pageBean<>();

        pageBean.setPage(brands);

        pageBean.setTotal(i);
        sqlSession.commit();

        sqlSession.close();

        return pageBean;
    }

    @Override
    public void deleteById(int id) {

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteById(id);

        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public void updateById(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.updateById(brand);

        sqlSession.commit();

        sqlSession.close();
    }

}
